/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package livro.control;

import bibliinfo.FrameMain;
import java.util.ArrayList;
import java.util.List;
import livro.model.Livro;
import livro.model.LivroDao;
import livro.model.LivroExe;
import livro.model.ValidaLivro;

/**
 *
 * @author Ar0n
 */
public class BookFrameDadosUpdate extends javax.swing.JInternalFrame {

    /**
     * Creates new form UserFrameDadosSalve
     */
    public BookFrameDadosUpdate() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        jButton3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/img_user/1365683699_window-close.png"))); // NOI18N
        jButton3.setText("Cancelar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/img_user/1365288060_3floppy_umount.png"))); // NOI18N
        jButton1.setText("Salvar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(380, Short.MAX_VALUE)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(45, 45, 45))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(58, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        
       FrameMain.bookFrameAltera.setVisible(false);
       FrameMain.bookFrameAltera.setEstado(false);
       
       FrameMain.bookFrameMain.setVisible(true);
       FrameMain.bookFrameMain.setEstado(true); 
       
       FrameMain.formata(21);
        
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        ValidaLivro valida = new ValidaLivro();
        boolean resp = valida.validaUpdate(FrameMain.bookFrameAltera.jTTitulo, FrameMain.bookFrameAltera.jTAutor,
                FrameMain.bookFrameAltera.jTEditora, FrameMain.bookFrameAltera.jTISBN, FrameMain.bookFrameAltera.jTQuant);
        
        if(resp == true){
            Livro livro = new Livro();
            livro.setISBN(FrameMain.bookFrameMain.ISBNSelecionado);
            livro.setNovoISBN(FrameMain.bookFrameAltera.jTISBN.getText());
            livro.setTitulo(FrameMain.bookFrameAltera.jTTitulo.getText());
            livro.setAutor(FrameMain.bookFrameAltera.jTAutor.getText());
            livro.setEditora(FrameMain.bookFrameAltera.jTEditora.getText());
            livro.setAtivo(true);
            
            List<LivroExe> lista = new ArrayList<>();
                        
            for(int i = 0; i < FrameMain.bookFrameAltera.defaultTable.getRowCount(); i ++){
                for(LivroExe e : FrameMain.bookFrameMain.listaLivroRealQuero){
                    int l = Integer.parseInt(FrameMain.bookFrameAltera.jTable.getValueAt(i, 0).toString());
                l = l -1;                
                if(l == e.getNumSequencia()){                    
                        lista.add(e);
                    }
                }               
            } 
            
            LivroDao dao = new LivroDao();
            dao.update(livro, lista);   
            
            FrameMain.bookFrameAltera.setVisible(false);
            FrameMain.bookFrameAltera.setEstado(false);
            
            FrameMain.bookFrameMain.setVisible(true);
            FrameMain.bookFrameMain.setEstado(true);
            FrameMain.formata(21);            
        }       
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    // End of variables declaration//GEN-END:variables
    //métodos
    public void altualizarLivro(){
        
        Livro livro = new Livro();
        LivroExe exe = new LivroExe();
        
        livro.setTitulo(FrameMain.bookFrameAltera.jTTitulo.getText());
        livro.setAutor(FrameMain.bookFrameAltera.jTAutor.getText());
        livro.setEditora(FrameMain.bookFrameAltera.jTEditora.getText());
        livro.setISBN(FrameMain.bookFrameAltera.jTISBN.getText());
        
    }

}
